<template>
  <div class="product-section comments-section">
    <h3>商品评论</h3>
    <div class="comment-form">
      <textarea
          v-model="newComment"
          placeholder="请输入您的评论..."
      ></textarea>
      <div class="emoji-picker">
        <span
            v-for="(emoji, index) in emojis"
            :key="index"
            :data-emoji="emoji"
            @click="addEmoji(emoji)"
        >
          {{ emoji }}
        </span>
      </div>
      <button class="submit-btn" @click="submitComment">提交评论</button>
    </div>
    <div class="comment-list">
      <div class="comment-item" v-for="(comment, index) in comments" :key="index">
        <div class="comment-header">
          <div class="comment-author">{{ comment.author }}</div>
          <div class="comment-date">{{ comment.date }}</div>
        </div>
        <div class="comment-content">{{ comment.content }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, defineProps, defineEmits } from 'vue'

export default {
  props: {
    comments: {
      type: Array,
      required: true
    }
  },
  emits: ['submitComment'],
  setup(props, { emit }) {
    const newComment = ref('')
    const emojis = ref(['😀', '😍', '👍', '❤️', '😂', '😊'])

    const addEmoji = (emoji) => {
      newComment.value += emoji
    }

    const submitComment = () => {
      if (newComment.value.trim()) {
        emit('submitComment', newComment.value)
        newComment.value = ''
      } else {
        alert('请输入评论内容！')
      }
    }

    return {
      newComment,
      emojis,
      addEmoji,
      submitComment
    }
  }
}
</script>

<style scoped>
.product-section {
  margin-top: 30px;
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}

.product-section h3 {
  font-size: 20px;
  margin-bottom: 15px;
  color: #212529;
  border-left: 4px solid #007BFF;
  padding-left: 10px;
}

.comments-section {
  margin-top: 30px;
}

.comment-form {
  margin-bottom: 30px;
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 8px;
}

.comment-form textarea {
  width: 100%;
  height: 100px;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  resize: none;
  margin-bottom: 10px;
  font-family: 'Microsoft YaHei', sans-serif;
}

.emoji-picker {
  margin-bottom: 10px;
}

.emoji-picker span {
  cursor: pointer;
  font-size: 20px;
  margin-right: 10px;
  display: inline-block;
}

.comment-list {
  margin-top: 20px;
}

.comment-item {
  padding: 15px 0;
  border-bottom: 1px solid #eee;
}

.comment-item:last-child {
  border-bottom: none;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.comment-author {
  font-weight: bold;
  color: #007BFF;
}

.comment-date {
  color: #6c757d;
  font-size: 14px;
}

.comment-content {
  color: #495057;
  line-height: 1.6;
}

.submit-btn {
  background-color: #007BFF;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
}

.submit-btn:hover {
  background-color: #0069d9;
}
</style>